package server.db;


import java.sql.Connection;
import java.sql.SQLException;

import org.apache.log4j.Logger;

import server.control.Settings;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.mchange.v2.log.MLevel;

public class ConnectionPool{
	
	static private final Logger logger = Logger.getLogger(ConnectionPool.class.getName());
	
	static private ComboPooledDataSource cpds = new ComboPooledDataSource();
	static private ConnectionPool myPool = null;
	
	ConnectionPool(){
		try{
			cpds.setDriverClass(Settings.jdbcDriver);
			cpds.setJdbcUrl(Settings.jdbcUrl);
			cpds.setUser(Settings.jdbcUser);
			cpds.setPassword(Settings.jdbcPwd);
			
		
		}catch(Exception e){
			logger.warn("Could not instantiate ConnectionPool");
		}

	}
	
	protected void finalize(){
		cpds.close();
	}
	
	static public ConnectionPool getInstance(){
		if(myPool == null){
			myPool = new ConnectionPool();
		}
		
		return myPool;
	}
	
	public Connection getConnection(){
		try{
			return cpds.getConnection();
		}catch(SQLException e){
			logger.warn("Couldn't load Conncetion to SQL DB. Exspect Null Pointer Exception");
			return null;
		}
	}
}
